package com.tiger.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiger.utils.UpdateUtils;
import com.tiger.mapper.homePage.*;
import com.tiger.pojo.homePage.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author HuXuehao (StudiousTiger)
 * @desc 这个类的作用是
 * @date 2022/2/11
 */
@Slf4j
@RestController
public class HomePageForOtherController {
    @Autowired
    private WorksCountsMapper worksCountsMapper;

    @Autowired
    private HotArticleMapper hotArticleMapper;
    @Autowired
    private HomePageErrorCardsMapper homePageErrorCardsMapper;
    @Autowired
    private HomePageQuestionCardsMapper homePageQuestionCardsMapper;
    @Autowired
    private HomePageBlinkCardsMapper homePageBlinkCardsMapper;
    @Autowired
    private UserGoodWorksMapper userGoodWorksMapper;
    @Autowired
    private UpdateUtils updateUtils;

    /**
     * 获取页码
     * @param classify
     * @return
     */
    @RequestMapping(value = "/homePage/getPages/{classify}", name = "获取用户主页相关部分的页码/user")
    public Long getPages(@PathVariable String classify) {
        if ("hotArticle".equals(classify)) {
            Page<HotArticle> page = new Page<>(1, 20);
            Page<HotArticle> pages = hotArticleMapper.selectPage(page, null);
            return pages.getPages();
        } else if ("errorArticle".equals(classify)) {
            Page<HomePageErrorCards> page = new Page<>(1, 20);
            Page<HomePageErrorCards> pages = homePageErrorCardsMapper.selectPage(page, null);
            return pages.getPages();
        } else if ("questionArticle".equals(classify)) {
            Page<HomePageQuestionCards> page = new Page<>(1, 20);
            Page<HomePageQuestionCards> pages = homePageQuestionCardsMapper.selectPage(page, null);
            return pages.getPages();
        } else if ("blinkCard".equals(classify)) {
            Page<HomePageBlinkCards> page = new Page<>(1, 20);
            Page<HomePageBlinkCards> pages = homePageBlinkCardsMapper.selectPage(page, null);
            return pages.getPages();
        }

        return null;
    }

    /**
     * 查找homePage中hot专栏的卡片的信息（分页）
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/homePage/hotArticle/{currentPage}", name = "查找homePage中hot专栏的卡片的信息（分页）/user")
    public String getHotArticle(@PathVariable int currentPage){
        try {
            Page<HotArticle> page = new Page<>(currentPage, 20);
            Page<HotArticle> hotArticlePage = hotArticleMapper.selectPage(page, null);
            List<HotArticle> res = hotArticlePage.getRecords();
            for (HotArticle re : res) {
                re.setUserHeadPhotoPath(updateUtils.getNewHeadPhotoURL(re.getUserHeadPhotoPath()));
            }
            if (res.size()!=0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            log.error(e.toString());
            return "false";
        }
    }

    /**
     * 查找homePage中error专栏的卡片的信息（分页）
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/homePage/errorArticle/{currentPage}", name = "查找homePage中error专栏的卡片的信息（分页）/user")
    public String getErrorArticle(@PathVariable int currentPage){
        try {
            Page<HomePageErrorCards> page = new Page<>(currentPage, 20);
            Page<HomePageErrorCards> hotArticlePage = homePageErrorCardsMapper.selectPage(page, null);
            List<HomePageErrorCards> res = hotArticlePage.getRecords();
            for (HomePageErrorCards re : res) {
                re.setHeadPhotoPath(updateUtils.getNewHeadPhotoURL(re.getHeadPhotoPath()));
            }
            if (res.size()!=0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            log.error(e.toString());
            return "false";
        }
    }

    /**
     * blink（分页）
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/homePage/blinkCard/{currentPage}", name = "查找homePage中error专栏的卡片的信息（分页）/user")
    public String getBlinkCard(@PathVariable int currentPage){
        try {
            Page<HomePageBlinkCards> page = new Page<>(currentPage, 20);
            QueryWrapper<HomePageBlinkCards> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("blink_like");
            Page<HomePageBlinkCards> hotArticlePage = homePageBlinkCardsMapper.selectPage(page, wrapper);
            List<HomePageBlinkCards> res = hotArticlePage.getRecords();
            for (HomePageBlinkCards re : res) {
                re.setHeadPhotoPath(updateUtils.getNewHeadPhotoURL(re.getHeadPhotoPath()));
            }
            if (res.size()!=0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            log.error(e.toString());
            return "false";
        }
    }

    /**
     * 查找homePage中question专栏的卡片的信息（分页）
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/homePage/questionArticle/{currentPage}", name = "查找homePage中question专栏的卡片的信息（分页）/user")
    public String getQuestionArticle(@PathVariable int currentPage){
        try {
            Page<HomePageQuestionCards> page = new Page<>(currentPage, 20);
            Page<HomePageQuestionCards> hotArticlePage = homePageQuestionCardsMapper.selectPage(page, null);
            List<HomePageQuestionCards> res = hotArticlePage.getRecords();
            for (HomePageQuestionCards re : res) {
                re.setHeadPhotoPath(updateUtils.getNewHeadPhotoURL(re.getHeadPhotoPath()));
            }
            if (res.size()!=0){
                return JSON.toJSONString(res);
            } else {
                return "empty";
            }
        } catch (Exception e) {
            log.error(e.toString());
            return "false";
        }
    }





    /**
     * 查找homePage页面中的error、question、blink的总数
     * @return
     */
    @RequestMapping(value = "/homePage/worksCounts", name = "查找homePage页面中的error、question、blink的总数/user")
    public String getWorksCounts(){
        List<WorksCounts> res = worksCountsMapper.selectList(null);

        return JSON.toJSONString(res);
    }

    /**
     * 查找homePage页面中的精品内容（9条）
     * @return
     */
    @RequestMapping(value = "/homePage/goodContent", name = "查找homePage页面中的精品内容（9条）/user")
    public String getHotArticle(){
        Page<UserGoodWorks> page = new Page<>(1, 9);
        QueryWrapper<UserGoodWorks> wrapper = new QueryWrapper<>();
        Page<UserGoodWorks> hotArticlePage = userGoodWorksMapper.selectPage(page, wrapper);
        List<UserGoodWorks> res = hotArticlePage.getRecords();

        return JSON.toJSONString(res);
    }
}
